package com.czc.mvc.handler;

import com.czc.entity.Admin;
import com.czc.entity.ParamData;
import com.czc.entity.Student;
import com.czc.service.api.AdminService;
import com.czc.util.CrowdUtil;
import com.czc.util.ResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class testHandler {

    @Autowired
    private AdminService adminService;

    Logger logger = LoggerFactory.getLogger(testHandler.class);

    @RequestMapping("/test/ssm.html")
    public String testSsm(ModelMap modelMap, HttpServletRequest request){

        boolean flag = CrowdUtil.judgeRequestType(request);
        logger.info("flag="+flag);

        List<Admin> list=adminService.getAll();
        modelMap.addAttribute("adminlist",list);
        System.out.println(10/0);

        /*//创造一个空指针异常
        String a=null;
        System.out.println(a.length());*/

        return "show";
    }

    @ResponseBody
    @RequestMapping("/send/array/one.html")
    public String testSendone(@RequestParam("array[]") List<Integer> array){
        for(Integer num:array){
            System.out.println("num="+num);
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping("/send/array/two.html")
    public String testSendtwo(ParamData paramData){
        List<Integer> array = paramData.getArray();
        for(Integer num:array){
            System.out.println("num="+num);
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping("send/array/three.html")
    public String testSendthree(@RequestBody List<Integer> array){


        for(Integer num:array){
            logger.info("num="+num);
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping("/send/comose/object.html")
    public String testSendobject(@RequestBody Student student){

       logger.info(student.toString());
        return "success";
    }


    @ResponseBody
    @RequestMapping("/send/comose/object/two.json")
    public ResultEntity<Student> testSendobject2(@RequestBody Student student,HttpServletRequest request){

        boolean flag = CrowdUtil.judgeRequestType(request);
        logger.info("flag="+flag);



        logger.info(student.toString());
        //将查询到的student对象封装到ResultEntity中返回
        ResultEntity<Student> resultEntity=ResultEntity.successWithData(student);

        //创造一个空指针异常
        String a=null;
        System.out.println(a.length());

        return resultEntity;
    }


}
